import { type VbenFormProps, type VbenFormSchema } from '#/adapter';

class FormConfigBuilder {
  private config: VbenFormProps;

  constructor(config: Omit<VbenFormProps, 'commonConfig'>) {
    this.config = {
      commonConfig: {
        // 在label后显示一个冒号
        colon: true,
        // 所有表单项
        componentProps: {
          class: 'w-full',
        },
      },
      resetButtonOptions: {
        show: false,
      },
      submitButtonOptions: {
        show: false,
      },
      ...config,
    };
  }

  // 返回配置
  build = (): VbenFormProps => {
    return this.config;
  };
}

export function withDefaultFormConfig(config: VbenFormProps) {
  return new FormConfigBuilder(config);
}

/**
 * 文本字段
 * @param field
 * @param label
 * @param options
 */
export function textField(
  field: string,
  label: string,
  options?: Partial<VbenFormSchema>,
): VbenFormSchema {
  return {
    component: 'Input',
    fieldName: field,
    label,
    componentProps: {
      placeholder: `请输入${label}`,
    },
    ...options,
  };
}

/**
 * 数字字段
 * @param field
 * @param label
 * @param options
 */
export function numberField(
  field: string,
  label: string,
  options?: Partial<VbenFormSchema>,
): VbenFormSchema {
  return {
    component: 'InputNumber',
    fieldName: field,
    label,
    componentProps: {
      placeholder: `请输入${label}`,
    },
    ...options,
  };
}

/**
 * 数字字段
 * @param field
 * @param label
 * @param compOptions
 * @param options
 */
export function apiTreeSelectField(
  field: string,
  label: string,
  compOptions: VbenFormSchema['componentProps'],
  options?: Partial<Omit<VbenFormSchema, 'componentProps'>>,
): VbenFormSchema {
  return {
    component: 'ApiTreeSelect',
    fieldName: field,
    label,
    componentProps: {
      placeholder: `请输入${label}`,
      treeDefaultExpandAll: true,
      childrenField: 'children',
      labelField: 'deptName',
      valueField: 'id',
      ...compOptions,
    },
    ...options,
  };
}

/**
 * 数字字段
 * @param field
 * @param label
 * @param compOptions
 * @param options
 */
export function selectField(
  field: string,
  label: string,
  compOptions?: VbenFormSchema['componentProps'],
  options?: Partial<Omit<VbenFormSchema, 'componentProps'>>,
): VbenFormSchema {
  return {
    component: 'Select',
    fieldName: field,
    label,
    componentProps: {
      placeholder: `请选择${label}`,
      labelField: 'deptName',
      valueField: 'id',
      options: [{ label: '测试', value: '1' }],
      ...compOptions,
    },
    ...options,
  };
}
